package com.cke.yang.security.api.controller;

import com.cke.yang.common.domain.model.Result;
import com.cke.yang.security.api.dto.request.ChangePasswordRequest;
import com.cke.yang.security.api.dto.request.SecUserListRequest;
import com.cke.yang.security.app.service.SecUserService;
import com.cke.yang.security.domain.entity.SecUser;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author yuyang
 */
@RestController
@RequestMapping("/api/security/sec-user")
public class SecUserController {

    @Autowired
    private SecUserService secUserService;

    @PostMapping("/list")
    public Result<PageInfo<SecUser>> list(@PageableDefault(page = 1) Pageable page, @RequestBody SecUserListRequest param) {
        return Result.success(secUserService.list(page, param));
    }

    @PostMapping("/save")
    public Result<List<SecUser>> save(@RequestBody List<SecUser> secUser) {
        return Result.success(secUserService.save(secUser));
    }

    @GetMapping("/self")
    public Result<?> self() {
        return Result.success(secUserService.self());
    }

    @PostMapping("/change-password")
    public Result<?> changePassword(@RequestBody ChangePasswordRequest request) {
        secUserService.changePassword(request.getOldPassword(), request.getNewPassword());
        return Result.success();
    }

}
